package com.info.zhiduoduo.repository.mapper.company;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.company.CompanyPayBalanceDto;
import com.info.zhiduoduo.repository.entity.company.CompanyParty;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author tianyonghong
 */
public interface CompanyPartyMapper extends BaseMapper<CompanyParty> {

    @Select(" select par.*,p.pay_type_sn,p.pay_name,p.source_id,p.source_table,c.name as cityName,ccp.company_name as partyName,tax.tax_source_name,pay.balance " +
            " from zdd_company_party par" +
            " left join zdd_payment p on par.pay_id = p.id " +
            " left join zdd_company_payment pay on par.pay_id = pay.pay_id and par.compid = pay.compid and pay.deleted = 0 " +
            " left join zdd_city_config c on par.city_id = c.id " +
            " left join zdd_crowdsource_company_party ccp on ccp.id = par.party_id " +
            " left join zdd_tax_source tax on tax. tax_source_id = par.tax_source_id " +
            " where par.compid = #{compid} " +
            " and if(ISNULL(#{businessSn}),1=1,par.business_sn = #{businessSn})" +
            " and par.deleted = 0 ")
    List<CompanyParty> getCompanyPartyList(Integer compid, String businessSn);

    @Select(" select par.*,p.pay_type_sn,p.pay_name,p.source_id,p.source_table,c.name as cityName,ccp.company_name as partyName,tax.tax_source_name " +
            " from zdd_company_party par" +
            " left join zdd_payment p on par.pay_id = p.id " +
            " left join zdd_city_config c on par.city_id = c.id " +
            " left join zdd_crowdsource_company_party ccp on ccp.id = par.party_id " +
            " left join zdd_tax_source tax on tax. tax_source_id = par.tax_source_id " +
            " where par.compid = #{compid} " +
            " and if(ISNULL(#{payId}),1=1,par.pay_id = #{payId})" +
            " and if(ISNULL(#{businessSn}),1=1,par.business_sn = #{businessSn})" +
            " and par.deleted = 0 ")
    List<CompanyParty> selectCompanyPartyList(Integer compid, Integer payId, String businessSn);

    // 根据企业ID+业务类型 获取企业默认的一条支付方式、主体
    @Select("  select par.*,p.pay_type_sn,p.pay_name,p.source_id,p.source_table,c.name as cityName,ccp.company_name as partyName,tax.tax_source_name " +
            "  from zdd_company_party par " +
            "  left join zdd_payment p on par.pay_id = p.id " +
            "  left join zdd_city_config c on par.city_id = c.id " +
            "  left join zdd_crowdsource_company_party ccp on ccp.id = par.party_id " +
            "  left join zdd_tax_source tax on tax. tax_source_id = par.tax_source_id " +
            "  where par.compid = #{compid}" +
            "  and par.business_sn = #{businessSn} and is_default = 1 " +
            "  and par.deleted = 0 limit 1")
    CompanyParty getCompanyDefault(Integer compid, String businessSn);

    // 查询企业所有支付方式下的总余额
    @Select(" select sum(pay.balance) from zdd_company_payment pay " +
            " where pay.compid = #{compid} " +
            " and pay.deleted = 0")
    BigDecimal getCompanyTotalBalance(Integer compid);

    // 增加查询企业余额
    @Select(" select par.*,p.pay_type_sn,p.pay_name,p.pay_name_alias,p.source_id,p.source_table,c.name as cityName,ccp.company_name as partyName,tax.tax_source_name," +
            " pay.balance,pay.frozen_balance " +
            " from zdd_company_party par" +
            " join zdd_payment p on par.pay_id = p.id " +
            " left join zdd_company_payment pay on par.pay_id = pay.pay_id AND par.compid = pay.compid AND pay.deleted = 0 " +
            " left join zdd_city_config c on par.city_id = c.id " +
            " left join zdd_crowdsource_company_party ccp on ccp.id = par.party_id " +
            " left join zdd_tax_source tax on tax. tax_source_id = par.tax_source_id " +
            " where par.compid = #{compid} " +
            " and if(ISNULL(#{businessSn}),1=1,par.business_sn = #{businessSn})" +
            " and if(ISNULL(#{isDefault}),1=1,par.is_default = #{isDefault})" +
            " and par.deleted = 0 ")
    List<CompanyPayBalanceDto> getCompanyWithBalance(Integer compid, String businessSn, Integer isDefault);

}
